package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dto.DtoParameter;
import com.qf.dto.GoodsDto;
import com.qf.entity.MallGoodsSpecEntity;
import com.qf.entity.MallGoodsSpecOptionsEntity;
import com.qf.service.impl.MallGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/MallGoods")
@Api(tags = "商品控制器")
public class MallGoodsController {
    @Autowired
    private MallGoodsService service;

    @PostMapping("/findByPage")
    @ApiOperation("分页查询")
    public PageInfo<MallGoodsSpecOptionsEntity> findByPage(@ApiParam("分页条件查询所有信息") @RequestBody DtoParameter<MallGoodsSpecOptionsEntity> dtoParameter) {
        PageHelper.startPage(Integer.parseInt(dtoParameter.getPage()), Integer.parseInt(dtoParameter.getSize()));
        List<MallGoodsSpecOptionsEntity> byPage = service.findByPage(dtoParameter.getWhere());
        PageInfo<MallGoodsSpecOptionsEntity> mallGoodsSpecOptionsEntityPageInfo = new PageInfo<>(byPage);
        return mallGoodsSpecOptionsEntityPageInfo;
    }

    @PostMapping("/add")
    @ApiOperation("新增商品")
    public int add(@ApiParam("新增商品信息对象")@RequestBody List<MallGoodsSpecOptionsEntity> entities) {
        if (entities == null) {
            return -1;
        }
        for (MallGoodsSpecOptionsEntity entity : entities) {
             service.add(entity.getGoodsSpec(), entities);
        }
        return 0;
    }

    @PostMapping("/delete")
    @ApiOperation("删除商品")
    public Map<String, Integer> delete(@ApiParam("删除id") String id) {
        if (id == null) {
            return new HashMap<String, Integer>() {{
                put("esc", -1);
            }};
        }
        int delete = service.delete(id);
        return new HashMap<String, Integer>() {{
            put("esc", delete);
        }};
    }

    @PostMapping("/update")
    @ApiOperation("修改商品信息")
    public Map<String, Integer> update(@ApiParam("修改对象信息")@RequestBody MallGoodsSpecOptionsEntity optionsEntity) {
        if (optionsEntity == null) {
            return new HashMap<String, Integer>() {
                {
                    put("esc", -1);
                }
            };
        }
        int update = service.update(optionsEntity);
        return new HashMap<String, Integer>() {{
            put("esc", update);
        }};
    }
}
